const { ccclass, property, menu, playOnFocus, inspector, executeInEditMode } = cc._decorator;

@ccclass
@menu('自定义组件/skeleton-animation')

export class SkeAnimation extends cc.Component {

    private maxPlayCount: number = 0//最大播放次数
    private playCount: number = 0//记录动画播放次数

    playAnimationByCount(count: number, aniName: string, skinName?: string, isloop?: boolean, callback?: any, caller?: any) {
        this.maxPlayCount = count
        this.playCount = 0
        skinName = skinName ? skinName : 'default'
        isloop = isloop ? isloop : false
        this.playAnimation(skinName, aniName, isloop, callback, caller)
    }


    playAnimation(skinName: string, aniName: string, isloop: boolean, callback?: any, caller?: any) {
        let ske_spine = this.node.getComponent(sp.Skeleton)
        ske_spine.setSkin(skinName)
        ske_spine.setAnimation(0, aniName, isloop)

        ske_spine.setCompleteListener((trackEntry) => {
            if (!isloop && trackEntry.animation.name === aniName) {
                this.playCount++
                if (this.playCount < this.maxPlayCount) {
                    this.playAnimation(skinName, aniName, isloop, callback, caller)
                } else {
                    callback && callback.call(caller)
                }
            }
        })
    }

}
